import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_text_field.dart';
import 'package:nyh_motorcade_app/widgets/precision_limit_formatter.dart';
import 'company_settlement_controller.dart';

class CompanySettlementPage extends StatelessWidget {
  final controller = Get.put(CompanySettlementController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "公司结算"),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 12.w,
                  ),
                  Material(
                    child: InkWell(
                      onTap: () {
                        controller.selectAccount();
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        height: 48.w,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text(
                                  "*",
                                  style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
                                ),
                                Text(
                                  "收款账户",
                                  style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                GetBuilder<CompanySettlementController>(
                                  builder: (controller) {
                                    return Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      children: [
                                        Text(
                                          controller.receivedAccountName,
                                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                                        ),
                                        Text(
                                          controller.receivedAccountCode,
                                          style: TextStyle(color: Colours.color_9fa0ab, fontSize: 12.sp),
                                        ),
                                      ],
                                    );
                                  },
                                ),
                                SizedBox(
                                  width: 8.w,
                                ),
                                Image.asset("images/ic_arrow_right_gray.png"),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    height: 48.w,
                    color: Colours.color_white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "付款账户",
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                        GetBuilder<CompanySettlementController>(
                          builder: (controller) {
                            return Text(
                              controller.paymentAccountName,
                              style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                            );
                          },
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    height: 48.w,
                    color: Colours.color_white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "可用余额(元)",
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                        GetBuilder<CompanySettlementController>(
                          builder: (controller) {
                            return Text(
                              FormatUtils.formatMoney(controller.companyAmount),
                              style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                            );
                          },
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 12.w,
                  ),
                  Container(
                    color: Colours.color_white,
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    height: 48.w,
                    child: Row(
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              "*",
                              style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
                            ),
                            Text(
                              "付款金额(元)",
                              style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                            ),
                          ],
                        ),
                        Expanded(
                          child: CustomTextField(
                            "请输入付款金额",
                            focusNode: controller.paymentAccountFocusNode,
                            textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                            hintStyle: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
                            fillColor: Colours.color_white,
                            contentPadding: EdgeInsets.symmetric(vertical: 15.w),
                            keyboardType: TextInputType.numberWithOptions(decimal: true),
                            textAlign: TextAlign.right,
                            inputFormatters: [
                              LengthLimitingTextInputFormatter(10),
                              FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
                              PrecisionLimitFormatter(2),
                            ],
                            onChanged: (value) {
                              controller.setPaymentAmount(value);
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 12.w,
                  ),
                  Container(
                    width: double.infinity,
                    color: Colours.color_white,
                    padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "备注",
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                        SizedBox(
                          height: 17.w,
                        ),
                        CustomTextField(
                          "请填写您的备注内容",
                          focusNode: controller.remarksFocusNode,
                          fillColor: Colours.color_background,
                          textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp, height: 1.5),
                          hintStyle: TextStyle(color: Colours.color_b7b7c0, fontSize: 14.sp),
                          contentPadding: EdgeInsets.only(left: 16.w, top: 16.w, right: 10.w, bottom: 16.w),
                          borderRadius: BorderRadius.circular(10.w),
                          keyboardType: TextInputType.multiline,
                          maxLines: 10,
                          minLines: 1,
                          inputFormatters: [LengthLimitingTextInputFormatter(100)],
                          onChanged: (value) {
                            controller.setRemarks(value);
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            color: Colours.color_white,
            padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 16.w),
            child: Row(
              children: [
                Expanded(
                  child: TextButton(
                    onPressed: () {
                      controller.addAccount();
                    },
                    child: Text(
                      "增加收款账户",
                      style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colours.color_white),
                      shape: MaterialStateProperty.all(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5.w),
                          side: BorderSide(width: 1.w, color: Colours.color_cfcfd5),
                        ),
                      ),
                      minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                    ),
                  ),
                ),
                SizedBox(
                  width: 16.w,
                ),
                Expanded(
                  child: TextButton(
                    onPressed: () {
                      controller.accountManage();
                    },
                    child: Text(
                      "收款账户管理",
                      style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colours.color_white),
                      shape: MaterialStateProperty.all(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5.w),
                          side: BorderSide(width: 1.w, color: Colours.color_cfcfd5),
                        ),
                      ),
                      minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                    ),
                  ),
                ),
                SizedBox(
                  width: 16.w,
                ),
                Expanded(
                  child: GetBuilder<CompanySettlementController>(
                    builder: (controller) {
                      return TextButton(
                        onPressed: () {
                          controller.confirm();
                        },
                        child: Text(
                          "确定",
                          style: TextStyle(color: Colours.color_white, fontSize: 14.sp),
                        ),
                        style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.resolveWith((states) {
                            return (controller.receivedAccountId.isNotEmpty && controller.paymentAmount > 0.0) ? Colours.color_theme : Colours.color_ffd599;
                          }),
                          overlayColor: MaterialStateProperty.resolveWith((states) {
                            return (controller.receivedAccountId.isNotEmpty && controller.paymentAmount > 0.0) ? Colours.color_theme_deep : Colours.color_ffd599;
                          }),
                          shape: MaterialStateProperty.all(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(5.w),
                            ),
                          ),
                          minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
